package com.bice.controller.entity;

import com.bice.controller.dao.IEntity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.*;

/**
 * 
 * @author Sirius
 */
@Entity
@Table(name="post")
public class Post  implements IEntity {
    
    private Integer id;
    private String code;
    private BiceUser from;
    private BiceUser to;
    private String message;
    private Date postAt;
    private Post parent;
    private List<Post> childs;
    
    public Post() {
    }
    
    @Id
    @GeneratedValue(strategy=IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    @Override
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(name="code", nullable=false, length=32)
    @Override
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @return the from
     */
    @ManyToOne
    public BiceUser getFrom() {
        return from;
    }

    /**
     * @param from the from to set
     */
    public void setFrom(BiceUser from) {
        this.from = from;
    }

    /**
     * @return the to
     */
    @ManyToOne
    public BiceUser getTo() {
        return to;
    }

    /**
     * @param to the to to set
     */
    public void setTo(BiceUser to) {
        this.to = to;
    }

    /**
     * @return the message
     */
    @Column(name="message", nullable=false, length=4096)
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the date
     */
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name="postAt")
    public Date getPostAt() {
        return postAt;
    }

    /**
     * @param postAt the date to set
     */
    public void setPostAt(Date postAt) {
        this.postAt = postAt;
    }

    /**
     * @return the parent
     */
    @ManyToOne
    public Post getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(Post parent) {
        this.parent = parent;
    }

    @OneToMany(mappedBy = "parent")
    public List<Post> getChilds() {
        return childs;
    }

    public void setChilds(List<Post> childs) {
        this.childs = childs;
    }
    
    @Transient
    public String getSign() {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:ss");
        return from.getNickname() + " - " + format.format(postAt);
    }
    
}